/**
 * 服务预约维护
 * @author miaoyl
 * @date 2019/04/03
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_where = {},
        cols = [[
            {type:'checkbox',fixed: 'left'},
            {field: 'ROW_ID',width: 80, title: '序号',fixed: 'left'},
            {field: 'MMRD001', width: 100, title: '任务类型', fixed: 'left',sort: true,
                filterDatas: app.datas_('crm_gwai740_mmrd001'),
                search:'default',searchOpts: {type: 'select', default: ''}}
            , {field: 'MMRDDOCNO', width: 180, title: '服务单号',sort: true, search:'default',searchOpts: {icon: true}}
            , {field: 'GWAK001',hide:true}
            , {field: 'GWAKL003', width: 160, title: '客户名称',sort: true}
            , {field: 'MMRDUSER_NAME', width: 160, title: '发起预约者姓名',sort: true,search:'default',searchOpts: {icon: true}}
            , {field: 'MMRD002', width: 160, title: '产品/设备编号'}
            , {field: 'MMRA004', width: 160, title: '品名'}
            , {field: 'MMRA006', width: 160, title: '规格'}
            , {field: 'MMRD003', width: 160, title: '产品/设备序列号'}
            , {field: 'MMRD004', width: 160, title: '预约服务日期',sort: true,search:'default',
                searchOpts: {type: 'daterange'}
            }
            , {field: 'MMRD005', width: 160, title: '预约服务时间段'}
            , {field: 'MMRD009', width: 160, title: '归属服务区'}
            , {field: 'MMRD010', width: 160, title: '联系人员姓名'}
            , {field: 'MMRD011', width: 160, title: '联系人员电话'}
            , {field: 'MMRD012', width: 160, title: '联系地址'}
            , {field: 'MMRD013', width: 160, title: '故障代码'}
            , {field: 'MMRD014', width: 160, title: '故障描述'}
            , {field: 'MMRD015', width: 160, title: '故障图片'}
            , {field: 'MMRD019', width: 160, title: '任务紧急程度',
                filterDatas: app.datas_('crm_gwai740_mmrd019')
            }
            , {field: 'MMRD021_NAME', width: 160, title: '受理人员'}
            , {field: 'MMRD022', width: 160, title: '受理日期'}
            , {field: 'MMRF007_NAME', width: 160, title: '派工者姓名'}
            , {field: 'MMRG005_NAME', width: 160, title: '现场回报者姓名'}
            , {field: 'MMRD023', width: 160, title: '是否上门',
                filterDatas: app.datas_('crm_gwai740_mmrd023')
            }
            , {field: 'MMRD024', width: 160, title: '预计上门服务日期'}
            , {field: 'MMRD025', width: 160, title: '预计上门服务时间'}
            , {field: 'MMRD027', width: 160, title: '结案日期时间'}
            , {field: 'MMRD016', width: 160, title: '任务来源',
                filterDatas: app.datas_('crm_gwai740_mmrd016')
            }
            , {field: 'MMRD020', width: 160, title: '任务状态',sort: true,
                filterDatas: app.datas_('crm_gwai740_mmrd020')
            }
            , {field: 'MMRD028', width: 160, title: '备注'}
            , {field: 'MMRDMODU_NAME', width: 160, title: '资料修改者'}
            , {field: 'MMRDACTI', width: 160, title: '有效否',filterDatas: app.datas_('sys_y_n')},
            {
                width: 60,
                title: '',
                toolbar: '#script-gwai740setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwai740SettingAction,
        moduleName = '服务预约'
    ;

    app.newModule({
        init: function () {
            self = this;
            let _searchsArr = app.cloneJSON(cols[0]);
            self._super({
                _tag: 'gwai740',
                _searchs: [{
                    id: 'gwai740', tableId: 'gwai740_table',
                    datas:  _searchsArr.pushArray([
                        { field: 'GWAKL003', width: 160, title: '客户',sort: true,
                            search: 'default', searchOpts:{icon:true}}
                    ])
                }],
                _loadTplsFull: [{
                    dom: 'gwai740_edit', tpl: 'gwai740_edit'
                },{
                    dom: 'gwai740_shouli', tpl: 'gwai740_shouli'
                },{
                    dom: 'gwai740_paigong', tpl: 'gwai740_paigong'
                },{
                    dom: 'gwai740_huifang', tpl: 'gwai740_huifang'
                }, {
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwai740_table'),
                    url: g_ctxPath + 'crm/gwai740/getMmrdList',
                    exportsUrl: g_ctxPath + 'crm/gwai740/getMmrdList',
                    exportsName: moduleName + '数据',
                    title: moduleName + '信息表',
                    id: 'gwai740_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: table_where
                });

                layui.laytpl(app.getByid(doc, 'script-gwai740setting-action').innerHTML).render({}, function (string) {
                    gwai740SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwai740_table)', function (obj) {

                    layer.tips(gwai740SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        if(obj.data.MMRD020 != '1'){
                                            app.showWarning("只有单据状态为1：发起才可以编辑!");
                                            return ;
                                        }
                                        self._openTplFull({
                                            tag: 'gwai740_edit',
                                            tagData: {
                                                moduleName: moduleName,
                                                obj:obj
                                            },
                                            index: 0,
                                            openOpts:{
                                                title: moduleName +'信息编辑',
                                                minmax: true
                                            }
                                        });
                                        break;
                                    case '1':
                                        self.delData(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行双击事件
                table.on('rowDouble(gwai740_table)', function (obj) {
                    if(obj.data.MMRD020 == '1'){
                        self._openTplFull({
                            tag: 'gwai740_edit',
                            tagData: {
                                is_detail: true,
                                moduleName:moduleName,
                                obj:obj
                            },
                            index: 0,
                            openOpts: {
                                title: moduleName + '信息编辑',
                                minmax: true
                            }
                        });
                    }else if(obj.data.MMRD020 == '2'){
                        self._openTplFull({
                            tag: 'gwai740_shouli',
                            tagData: {
                                is_detail: true,
                                moduleName:moduleName,
                                obj:obj.data
                            },
                            index: 1,
                            openOpts: {
                                title: moduleName + '信息编辑',
                                minmax: true
                            }
                        });
                    }else if(obj.data.MMRD020 == '3'){
                        self._openTplFull({
                            tag: 'gwai740_paigong',
                            tagData: {
                                is_detail: true,
                                moduleName:moduleName,
                                obj:obj.data
                            },
                            index: 2,
                            openOpts: {
                                title: moduleName + '信息编辑',
                                minmax: true
                            }
                        });
                    }else if(obj.data.MMRD020 == '6'){
                        self._openTplFull({
                            tag: 'gwai740_huifang',
                            tagData: {
                                is_detail: true,
                                moduleName:moduleName,
                                obj:obj.data
                            },
                            index: 3,
                            openOpts: {
                                title: moduleName + '信息编辑',
                                minmax: true
                            }
                        });
                    }
                });

                //监听事件
                table.on('toolbar(gwai740_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwai740_edit',
                                tagData: {
                                    is_add: true,
                                    moduleName:moduleName
                                },
                                index: 0,
                                openOpts: {
                                    title: moduleName + '信息编辑',
                                    minmax: true
                                }
                            });
                            break;
                        case 'del':
                            self.delDataBatch();
                            break;
                        case 'accept':
                            self.shouli();
                            break;
                        case 'paigong':
                            self.paigong();
                            break;
                        case 'huifang':
                            self.huifang();
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwai740_table', where: table_where});
        },

        delData: function (obj) {
            if(obj.data.MMRD020 != '1'){
                app.showWarning("只有单据状态为1：发起才可以删除!");
                return ;
            }
            app.confirm({
                msg: '是否删除选择的信息？',
                ok: function () {
                    app.ajax({
                        xurl: 'crm/gwai740/del',
                        xdata: obj.data,
                        loading: true,
                        okmsg: moduleName + '删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            });

        },

        delDataBatch: function(){
            var checkStatus = table.checkStatus('gwai740_table');
            var flag = true;
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    if(v.MMRD020 != '1'){
                        app.showWarning("只有单据状态为1：发起才可以删除!");
                        flag = false;
                        return ;
                    }
                    var _temp = app.fields_('MMRD_T');
                    _temp.MMRDDOCNO = v.MMRDDOCNO;
                    _temp.MMRDSEQ = v.MMRDSEQ;
                    _temp.MMRDENT = v.MMRDENT;
                    _xdata.push(_temp);
                });
                if(flag) {
                    app.ajax({
                        xurl: 'crm/gwai740/delDataBatch',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        confirm: {msg: "是否删除选择的数据？"},
                        okmsg: moduleName + '信息删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            }
        },

        shouli:function () {//受理
            var flag = true;
            var checkStatus = table.checkStatus('gwai740_table');
            if(checkStatus.data.length > 1){
                app.showMessage("仅允许单笔操作！");
                return;
            }
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    if (v.MMRD020 != '1') {
                        app.showWarning("任务状态不为【1 发起】，不可受理！");
                        flag = false;
                        return;
                    }
                });
            }else {
                app.showMessage("请先选择单据！");
                return;
            }
            if(flag) {
                self._openTplFull({
                    tag: 'gwai740_shouli',
                    tagData: {
                        obj: checkStatus.data[0]
                    },
                    index: 1,
                    openOpts: {
                        title: '受理服务单预约',
                        minmax: true
                    }
                });
            }
        },

        paigong:function () {//派公
            var flag = true;
            var checkStatus = table.checkStatus('gwai740_table');
            if(checkStatus.data.length > 1){
                app.showMessage("仅允许单笔操作！");
                return;
            }
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    if (v.MMRD020 != '2') {
                        app.showWarning("任务状态不为【2 受理】,不可派工!");
                        flag = false;
                        return;
                    }
                });
            }else {
                app.showMessage("请先选择单据！");
                return;
            }
            if(flag) {
                self._openTplFull({
                    tag: 'gwai740_paigong',
                    tagData: {
                        obj: checkStatus.data[0]
                    },
                    index: 2,
                    openOpts: {
                        title: '服务派工',
                        minmax: true
                    }
                });
            }
        },

        huifang:function () {//派公
            var flag = true;
            var checkStatus = table.checkStatus('gwai740_table');
            if(checkStatus.data.length > 1){
                app.showMessage("仅允许单笔操作！");
                return;
            }
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    if (v.MMRD020 != '5') {
                        app.showWarning("任务状态不为【5 评价】,不可进行回访!");
                        flag = false;
                        return;
                    }
                });
            }else {
                app.showMessage("请先选择单据！");
                return;
            }
            if(flag) {
                self._openTplFull({
                    tag: 'gwai740_huifang',
                    tagData: {
                        obj: checkStatus.data[0]
                    },
                    index: 3,
                    openOpts: {
                        title: '回访结案',
                        minmax: true
                    }
                });
            }
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwai740',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field == 'MMRDDOCNO') {//核销单号
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            columnList: [
                                                {label: '服务预约单号', prop: 'MMRDDOCNO' , width: '150'},
                                                {label: '单据日期', prop: 'MMRDDOCDATE', width: '150'},
                                                {label: '人员', prop: 'MMRD010', width: '150'},
                                                {label: '状态', prop: 'MMRDACTI_NAME', width: '150'}
                                            ],
                                            url:"crm/gwai740/getMmrddocno"
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.MMRDDOCNO = data.MMRDDOCNO;
                                        }
                                    },
                                    index: 4,
                                    openOpts: {
                                        title: '选择服务预约单号',
                                        area: ['600px', '470px']
                                    }
                                });
                            }else if(field == 'GWAKL003'){//客户编号
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_gwak001',
                                            columnList: [
                                                {label: '客户编号', prop: 'GWAK001' , width: '150'},
                                                {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                                {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWAKL003 = data.GWAK001;
                                        }
                                    },
                                    index: 4,
                                    openOpts: {
                                        title: '选择客户',
                                        area: ['600px', '470px']
                                    }
                                });
                            }else if(field == 'MMRDUSER_NAME'){//发起者姓名
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_sysuser_id',
                                            columnList: [
                                                {label: '编号', prop: 'ID' , width: '150'},
                                                {label: '姓名', prop: 'NAME', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.MMRDUSER_NAME = data.ID;
                                        }
                                    },
                                    index: 4,
                                    openOpts: {
                                        title: '选择发起者',
                                        area: ['600px', '470px']
                                    }
                                });
                            }
                        }
                    },
                    watch: {
                        'isearch.fields.MMRD004': function (newValue, oldValue) {
                            if (newValue != null && newValue.length == 2) {
                                this.isearch.fields.MMRD004_START = newValue[0].replace(/-/g, '');
                                this.isearch.fields.MMRD004_END = newValue[1].replace(/-/g, '');
                            } else {
                                this.isearch.fields.MMRD004_START = '';
                                this.isearch.fields.MMRD004_END = '';
                            }
                        }
                    }
                }]
            });
            self.initTable();
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

});